String.prototype.splice = function(start, newStr) {
  return this.slice(0, start) + newStr + this.slice(start);
};

/**
 * 用于交互的 x组件，即 x空间
 */
export default {
  computed1: {
    /**
     * 名称
     */
    xtitle() {
      return this.getXMeta('xtitle')
    },
    /**
     * 子元素名称
     */
    xtitleItem() {
      return (index, xcomponentsMetaPath='xcomponents')=>this.getXMeta(xcomponentsMetaPath+'.'+index+'.xtitle')
    },
    /**
     * 数据字段，指操作数据对象的字段
     */
    xfield() {
      return this.getXMeta('xfield')
    },
    /**
     * 子元素数据字段，指操作数据对象的字段
     */
    xfieldItem() {
      return (index, xcomponentsMetaPath='xcomponents')=>this.getXMeta(xcomponentsMetaPath+'.'+index+'.field')
    },
    /**
     * 是否只读，默认 false
     */
    xreadonly() {
      return this.getXMeta('xreadonly')===true
    },
    /**
     * 参考数据
     */
    xoptions() {
      return this.getXMeta('xoptions', [])
    },
  },
  methods: {
    // ======== 值 =========== //
    /**
     * 获取控件的主值
     */
    getXValue() {
      let meta = this.getXMeta('xvalue')
      if(meta&&typeof meta==='function') {
        let myxdata = this.getXData()
        return meta(this.myxmeta, myxdata, this)
      }else if(meta!==undefined) {
        return meta
      }else{
        return this.getXData(this.xfield)
      }
    },
    /**
     * 获取控件的子元素的主值
     */
    getXValueItem(index, xcomponentsMetaPath='xcomponents') {
      let meta = this.getXMeta(xcomponentsMetaPath+'.'+index+'.xvalue')
      if(meta&&typeof meta==='function') {
        let myxdata = this.getXData()
        return meta(this.myxmeta,myxdata, this)
      }else if(meta!==undefined) {
        return meta
      }else{
        return this.getXData(this.getXMeta(xcomponentsMetaPath+'.'+index+'.xfield'))
      }
    },
    /**
     * 获取控件的显示值
     */
    getXShow() {
      let val = this.getXValue()
      let meta = this.getXMeta('xshow', undefined, {extra: val})
      let ret = meta!==undefined?meta:val;

      if(ret===undefined) ret = this.getXMeta('xshowDefault')
      let xshowBefore = this.getXMeta('xshowBefore')
      if(xshowBefore) ret = xshowBefore + ret
      let xshowAfter = this.getXMeta('xshowAfter')
      if(xshowAfter) ret = ret + xshowAfter
      return ret;
    },
    getXShowFromOptions(field='label') {
      let xoptions = this.getXMeta('xoptions')
      if(!Array.isArray(xoptions)) return
      let ret = xoptions.find(v=>v.value===this.getXShow())
      return ret&&ret[field]
    },

    // ======== 事件 =========== //
    /**
     * 点击事件
     * @param {*} event 事件名后缀
     * @param {*} args 事件数据
     */
    xonClick(event='', ...args) {
      if(this.emitMyXCallBack('onBeforeClick'+event, this, ...args)) return
      this.emitMyXCallBack('onClick'+event, this, ...args)
    },
    /**
     * 数据改变事件
     * @param {*} e 值
     */
     xonChange(e, xfield=this.getXMeta('xfield')) {
      if(this.emitMyXCallBack('onBeforeChange', this, e)) return true
      this.setXData(xfield, e)
      this.emitMyXCallBack('onChange', this, e)
    },
    /**
     * 数据改变事件
     * @param {*} e 额外值
     */
     xonChangeExtra(e) {
      let myxdata = this.getXData()
      if(this.emitMyXCallBack('onBeforeChangeExtra', this.myxmeta, myxdata, this, e)) return true
      let extraField = ''
      if((this.xfield||'').includes('.')) {
        let index = (this.xfield||'').lastIndexOf('.')
        extraField = (this.xfield||'').splice(index+1, '_')
      }else{
        extraField = '_'+this.xfield
      }
      this.setXData(extraField, e, this.xdatapath)
      this.emitMyXCallBack('onChangeExtra', this.myxmeta, myxdata, this, e)
    },
    /**
     * 子元素点击事件
     * @param {*} event 事件名后缀
     * @param {*} item 子元素数据
     */
    xonClickByItem(event='', item, ...args) {
      if(this.emitChildXCallBack('onBeforeClick'+event, item, this, ...args)) return
      this.emitChildXCallBack('onClick'+event, item, this, ...args)
    },
    /**
     * options 元素点击事件
     * @param {*} event 事件名后缀
     * @param {*} index 子元素在options 的索引
     */
    xonClickByOptions(event='', index, optionsPath='xoptions') {
      let options = this.getXMeta(optionsPath)
      let data = options&&options[index]
      let myxdata = this.getXData()
      if(this.emitMyXCallBack('onBeforeClick'+event, this.myxmeta, myxdata, this, data)) return
      this.emitMyXCallBack('onClick'+event, this.myxmeta, myxdata, this, data)
    },
    
    
  }
}
